import { childrenProps } from '@/components/ProForm/interface';
import Decimal from 'decimal.js';

/**
 * @description 将数组格式内容转字符串拼接
 */
export const arrSimpleJoinToStr = (arr: Array<number | string>, flag = ','): string => {
  return arr?.length ? arr.join(flag) : '';
};

/**
 * @description 将复杂数组中某个字段转字符串拼接
 */
export function arrJoinToStr(arr: Array<any>, field: string): string {
  if (!arr || !arr.length) {
    return '';
  }
  const newArr = arr.map(item => item[field]);
  return newArr.join(',');
}

/**
 * @description 将复杂数组中某个字段提取出来
 */
export function arrLiftField(arr: Array<any>, field: string): Array<number | string> {
  if (!arr || !arr.length) {
    return [];
  }
  const newArr = arr.map(item => item[field]);
  return newArr;
}

/**
 * @param {Array} originArr 原数组
 * @param {String} label 显示的字段
 * @param {String} value 提交的内容
 * @description 转换为下拉框需要数据 和表格相关枚举
 */
export const arrTransitionSelectData = (
  originArr: Array<any>,
  label: string,
  value: string = '',
  other?: {
    disableValueType?: boolean;
  }
): any => {
  if (!originArr || !originArr.length) {
    return [];
  }
  const newArr = originArr.map(item => {
    // 数据字典中存储的值类型
    const { valueType, tagType } = item || {};
    const v = value.split(',');
    let useValue: any = [];
    if (v.length > 1) {
      for (let str of v) {
        useValue.push(item[str]);
      }
      useValue = useValue.join(',');
    } else {
      useValue = item[value] || item[label];
    }
    // 值类型转换 默认占用 valueType 字段
    if (valueType && !other?.disableValueType) {
      useValue = valueType === 'number' ? Number(useValue) : useValue;
    }

    const obj: any = {
      label: item[label],
      value: useValue,
      type: 'option'
    };
    if (tagType) {
      obj.tagType = tagType;
    }

    return obj;
  });

  return newArr;
};
/**
 * 树结构数据装换下拉框所需的数据
 */
export function treeToSelect(data: any[], label: string, value: string) {
  const newTree = [];
  for (let item of data) {
    newTree.push({
      label: item[label],
      value: item[value],
      children: item.children ? treeToSelect(item.children, label, value) : []
    });
  }
  return newTree;
}

/**
 * @param {String} value 提交的内容
 * @description 数组字段求和
 */
export const arrSummation = (data: Array<any>, field: string) => {
  if (!data?.length) return 0;
  let sum = 0;
  for (let item of data) {
    if (item[field]) {
      sum = new Decimal(sum).add(item[field]).toNumber();
    }
  }
  return sum;
};

/**
 * @description: 根据指定字段进行排序
 * @param {Array} arr
 * @param {string} field
 * @param {boolean} sortFlag 默认升序
 * @return {*}
 */
export const arrSortItemsByName = (arr: Array<any>, field: string, sortFlag: boolean = true) => {
  arr.sort((a, b) => {
    // 比较两个对象的name属性
    if (a[field] < b[field]) {
      return !sortFlag ? -1 : 1;
    }
    if (a[field] > b[field]) {
      return !sortFlag ? 1 : -1;
    }
    // 如果名称相同，返回0，sort()方法会保持它们的原始顺序
    return 0;
  });
};

/**
 * 对数组中的所有数字添加千分号
 * @param arr 数据
 */
export const arrRoundingFunction = (arr: any[]) => {
  arr.forEach(item => {
    Object.keys(item).forEach(key => {
      if (typeof item[key] === 'number') {
        item['locale_' + key] = item[key].toLocaleString();
      }
    });
  });
};

/**
 * 对数组中的所有数字进行取整千分号
 */
export const arrRoundOff = (arr: any[]) => {
  arr.forEach(item => {
    Object.keys(item).forEach(key => {
      if (typeof item[key] === 'number') {
        item[key] = Math.round(item[key]);
      }
    });
  });
};

/**
 * @description: 更具数组中指定字段进行合并  并对指定字段数量进行求和
 * @param {any} data
 * @param {string} mergeField
 * @param {string} sumField
 */

export const arrMergeRow = (data: any[], mergeField: string, sumField: string) => {
  if (!data?.length) return [];
  let lodData = JSON.parse(JSON.stringify(data));
  let newArr = [];
  let names = [];

  for (let item of lodData) {
    const index = names.indexOf(item[mergeField]);
    if (index === -1) {
      names.push(item[mergeField]);
      newArr.push(item);
    } else {
      const arr = sumField.split(',');
      for (let key of arr) {
        let a1 = newArr[index][key] || 0;
        let a2 = item[key] || 0;
        newArr[index][key] = new Decimal(a1).add(new Decimal(a2)).toNumber();
      }
    }
  }
  return newArr;
};

export const arrOperation = {
  arrMergeRow,
  arrRoundOff,
  arrRoundingFunction,
  arrSortItemsByName,
  arrSummation,
  arrTransitionSelectData,
  arrSimpleJoinToStr,
  arrJoinToStr,
  arrLiftField,
  treeToSelect
};
